package bTree.LeetCode;

import java.util.HashMap;

public class 连续数组 {
    public int findMaxLength(int[] nums) {
        int length = nums.length;
        int []preSum=new int[length+1];
        preSum[0]=0;
        for (int i = 1; i <= nums.length; i++) {
            preSum[i]=preSum[i-1]+(nums[i-1]==1?1:-1);
        }
        HashMap<Integer,Integer>hashMap=new HashMap<>();
        int max=0;
        for (int i = 0; i < preSum.length; i++) {
            if(!hashMap.containsKey(preSum[i])){
                hashMap.put(preSum[i],i);
            }else {
                max=Math.max(max,i-hashMap.get(preSum[i]));
            }
        }
        return max;
    }
}